<template>
  <div class="app-container">
    <div
      class="header"
      style="display: flex; justify-content: space-between"
    >
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item>
          <el-button
            icon="refresh"
            color=" rgb(69, 76, 105)"
            style="color: #fff"
            @click="resetQuery()"
          />
        </el-form-item>
      </el-form>
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item
          label=""
          prop=""
        >
          <el-input
            placeholder="请选择问题类型"
            v-model="queryParams.questionName"
          ></el-input>
        </el-form-item>
        <el-form-item
          label=""
          prop=""
        >
          <el-date-picker
            clearable
            v-model="queryParams.publishTime"
            type="daterange"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="YYYY-MM-DD"
            placeholder="请选择发布时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            type="info"
            icon="Search"
            @click="handleQuery()"
          />
        </el-form-item>
      </el-form>
    </div>
    <el-table
      v-loading="loading"
      :data="recordsList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
      />
      <el-table-column
        label="购买记录ID"
        align="center"
        prop="purchaseId"
      />
      <el-table-column
        label="套餐ID"
        align="center"
        prop="comboId"
      />
      <el-table-column
        label="司机ID"
        align="center"
        prop="driverId"
      />
      <el-table-column
        label="购买金额"
        align="center"
        prop="purchaseAmount"
      />
      <el-table-column
        label="购买方式"
        align="center"
        prop="purchaseWay"
      />
      <el-table-column
        label="购买时间"
        align="center"
        prop="purchaseTime"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.purchaseTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="到期时间"
        align="center"
        prop="endTime"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.endTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:records:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:records:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改购买记录对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      width="500px"
      append-to-body
    >
      <el-form
        ref="recordsRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item
          label="套餐ID"
          prop="comboId"
        >
          <el-input
            v-model="form.comboId"
            placeholder="请输入套餐ID"
          />
        </el-form-item>
        <el-form-item
          label="司机ID"
          prop="driverId"
        >
          <el-input
            v-model="form.driverId"
            placeholder="请输入司机ID"
          />
        </el-form-item>
        <el-form-item
          label="购买金额"
          prop="purchaseAmount"
        >
          <el-input
            v-model="form.purchaseAmount"
            placeholder="请输入购买金额"
          />
        </el-form-item>
        <el-form-item
          label="购买方式"
          prop="purchaseWay"
        >
          <el-input
            v-model="form.purchaseWay"
            placeholder="请输入购买方式"
          />
        </el-form-item>
        <el-form-item
          label="购买时间"
          prop="purchaseTime"
        >
          <el-date-picker
            clearable
            v-model="form.purchaseTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择购买时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item
          label="到期时间"
          prop="endTime"
        >
          <el-date-picker
            clearable
            v-model="form.endTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择到期时间"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button
            type="primary"
            @click="submitForm"
            >确 定</el-button
          >
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Records">
import {
  listRecords,
  getRecords,
  delRecords,
  addRecords,
  updateRecords,
} from "@/api/system/combo/records";

const { proxy } = getCurrentInstance();

const recordsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    comboId: null,
    driverId: null,
    purchaseWay: null,
    purchaseTime: null,
  },
  rules: {
    comboId: [{ required: true, message: "套餐ID不能为空", trigger: "blur" }],
    driverId: [{ required: true, message: "司机ID不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询购买记录列表 */
function getList() {
  loading.value = true;
  listRecords(queryParams.value).then((response) => {
    recordsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    purchaseId: null,
    comboId: null,
    driverId: null,
    purchaseAmount: null,
    purchaseWay: null,
    purchaseTime: null,
    endTime: null,
  };
  proxy.resetForm("recordsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.purchaseId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加购买记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _purchaseId = row.purchaseId || ids.value;
  getRecords(_purchaseId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改购买记录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["recordsRef"].validate((valid) => {
    if (valid) {
      if (form.value.purchaseId != null) {
        updateRecords(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addRecords(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _purchaseIds = row.purchaseId || ids.value;
  proxy.$modal
    .confirm('是否确认删除购买记录编号为"' + _purchaseIds + '"的数据项？')
    .then(function () {
      return delRecords(_purchaseIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "system/records/export",
    {
      ...queryParams.value,
    },
    `records_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
